Skip to content

fix(api): expire sandboxes and reclaim the files nothing references - #359

Merged
tas50 merged 2 commits into
mainfrom
fix/expire-sandboxes
Nov 29, 2026
Merged

tas50 merged 2 commits into
mainfrom
fix/expire-sandboxes

Conversation

@tas50

@tas50 tas50 commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

A file uploaded through a sandbox stayed in the blob store until a manifest referencing it was deleted or overwritten. Uncommitted sandboxes never expired and no route lists or deletes blobs, so any org member with create on sandboxes could fill server memory (memory backend) or disk (SQLite) with files nothing used. Only deleting the org got that space back.

  • A sandbox expires two hours after it is created or, once committed, two hours after the commit. That is twice the upload grant's TTL, so no file can be uploaded after its sandbox stops protecting it.
  • api.SweepSandboxes deletes expired sandboxes and the files they announced that no cookbook/artifact manifest references and no open sandbox still names. The server runs it every ten minutes. Library users call it themselves.
  • A sandbox stored without a timestamp (committed by an earlier version) counts as expired, so leftovers from before the upgrade are cleaned up too.
  • Committing an expired sandbox returns 404.
  • Each org has a blob-reference lock (blobrefs.go).
    • Held shared from the blob check through the write that records the dependence: createSandbox, commitSandbox, cookbook PUT, artifact PUT.
    • Held exclusively from the reference scan through the deletes: the sweep and every gcOrphanedBlobs call. This includes the existing GC on cookbook/artifact overwrite and delete, which had the same race on main.
    • Before, a manifest PUT landing between a GC's scan and its delete was stored pointing at a deleted file, and a sandbox announced during a sweep could be told needs_upload: false for a file the sweep then deleted.
    • File uploads take no lock: the sweep never deletes a file an open sandbox names.

erchef keeps uncommitted sandboxes and their uploads forever. Bookshelf's bksw_cleanup_task only reaps uploads that never completed, and committed checksums are deleted only along with the cookbook/artifact version that referenced them. So this is a deliberate difference, noted in the README. The differential script does not commit sandboxes, so the baseline is unchanged.

Unchanged: the sandbox and commit responses, the manifest checksum check, and which blobs GC on cookbook/artifact delete and overwrite removes. knife/chef upload, commit and PUT the manifest in one run.

Not addressed: no per-org quota (erchef has none). A member who keeps re-announcing a sandbox keeps its files alive, but removing that member from the org ends it within two hours.

Tests:

  • Expiry: TestSandboxSweepCollectsUnreferencedUploads, TestSandboxSweepKeepsFilesOfOpenSandboxes, TestSandboxSweepCountsFromCommit (each on memory and SQLite), TestSandboxSweepReclaimsUntimedSandboxes, TestServerSweepsExpiredSandboxes.
  • Ordering, through a hook between the GC's reference scan and its deletes, on memory and SQLite: TestSandboxCreateDuringSweepKeepsTheBlob, TestManifestPutDuringSweepKeepsTheBlob, TestManifestPutDuringOverwriteGCKeepsTheBlob. All three failed before the lock.

Overlap: #346 also edits internal/api/cookbooks.go (getFile/blob reads; this PR touches the sandbox handlers, the cookbook PUT and the GC), and #347 edits server/server.go and README.md. Any conflicts should be textual only. #353 adds per-org locks for group membership; this lock is separate from that one.

@tas50
tas50 force-pushed the fix/expire-sandboxes branch 2 times, most recently from d221dd0 to 1d7a54f Compare November 29, 2026 22:43
A file uploaded through a sandbox stayed in the blob store forever
unless a manifest that referenced it was later deleted or overwritten.
Uncommitted sandboxes never expired, and no route lists or deletes
blobs, so one org member could fill server memory (the default backend)
or disk with files nothing used, and only deleting the org reclaimed
them.

A sandbox now expires two hours after it is created or, once committed,
after the commit (twice the upload grant's lifetime, so a file cannot be
uploaded after its sandbox stops protecting it). A running server sweeps
every ten minutes: it deletes expired sandboxes and the files they
announced that no cookbook or artifact manifest references and no open
sandbox still names. Sandboxes stored without a timestamp by earlier
versions count as expired.

erchef keeps uncommitted sandboxes and their uploads indefinitely
(bookshelf's cleanup only reaps uploads that never completed), so this
is a deliberate difference, noted in the README.

Signed-off-by: Tim Smith <tsmith84@proton.me>
A blob GC read which checksums were still referenced and then deleted
the rest, with nothing ordering it against the handlers that check a
blob exists and then record a dependence on it. A manifest PUT that
passed its upload check and stored its manifest in between left the
manifest referencing a deleted file, whose download URL then 404s. The
sandbox sweep had the same gap, and also a sandbox announced after the
sweep read the open sandboxes could be told a file needs no upload just
before the sweep deleted it. The GC that overwriting or deleting a
cookbook or artifact version runs had the same race already.

Each org now has a blob-reference lock. createSandbox, commitSandbox
and the cookbook and artifact PUTs hold it shared from their blob checks
through the write that records the dependence; gcOrphanedBlobs and the
sweep hold it exclusively from reading references through the deletes.
A handler that runs a GC releases its shared hold first. File uploads
take no lock, since the sweep never deletes a file an open sandbox
names.

Signed-off-by: Tim Smith <tsmith84@proton.me>
@tas50
tas50 force-pushed the fix/expire-sandboxes branch from 1d7a54f to 18cd975 Compare November 29, 2026 22:45
@tas50
tas50 merged commit 4122af3 into main Nov 29, 2026
6 checks passed
@tas50
tas50 deleted the fix/expire-sandboxes branch November 29, 2026 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant